package org.example.cake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.C;
import org.example.cake.common.Rep;
import org.example.cake.entity.Carousel;
import org.example.cake.mapper.CarouselMapper;
import org.example.cake.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Console;
import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/carousel")
public class CarouselController {
   //Minio对象
    @Autowired
    MinioClient minioClient;

    @Autowired
    private CarouselService carouselService;
    /**
     * 获取所有轮播图信息
     * @return rep对象
     */
    @GetMapping("/getCarouselList")
    public Rep getCarouselList(){
        List<Carousel> carouselList = this.carouselService.getCarouselList();
        return new Rep().addData("carouselList", carouselList);
    }

    /**
     * //后台轮播图查询
     * @author 徐
     * @param title 轮播图标题
     * @date 2025-07-28
     * @return 轮播图集合
     */
    @GetMapping("/selectCarouselPageEnd")
    public Rep selectCarouselPageEnd(@RequestParam(defaultValue = "1") int page,
                                     @RequestParam(required = false) String title) {
        PageHelper.startPage(page, 5);
        List<Carousel> list = carouselService.selectCarouselPageEnd(title);
        PageInfo<Carousel> pageInfo = new PageInfo<>(list);

        return new Rep().addData("list", pageInfo.getList()).addData("total", pageInfo.getTotal());
    }

    /**
     * //后台轮播图删除（根据id删除）
     * @author 徐
     * @param id 轮播图id
     * @date 2025-07-28
     * @return 轮播图id
     */
    @PostMapping("/deleteCarouselIdEnd")
    public String deleteCarouselIdEnd(@RequestParam("id") Integer id) {
        System.out.println("删除的id====="+id);
        return carouselService.deleteCarouselIdEnd(id);
    }

    /**
     * //后台轮播图修改状态（根据id修改）
     * @author 徐
     * @param id 轮播图id
     * @param status 轮播图状态
     * @date 2025-07-28
     * @return 受影响的行数
     */
    @PostMapping("/updateCarouselStatusEnd")
    public String updateCarouselStatusEnd(@RequestParam("id") Integer id,@RequestParam("status") Integer status) {
        System.out.println("id："+id+"状态id:"+status);
        return carouselService.updateCarouselStatusEnd(id, status);
    }

    /**
     * //新增后台轮播图信息
     * @author 徐
     * @param carousel 轮播图对象
     * @date 2025-07-28
     * @return 受影响的行数
     */
    @PostMapping("/insertCarouselEnd")
    public String insertCarouselEnd(@RequestBody Carousel carousel) {
        return carouselService.insertCarouselEnd(carousel);
    }

    /**
     * //修改后台轮播图信息
     * @author 徐
     * @param carousel 轮播图对象
     * @date 2025-07-28
     * @return 受影响的行数
     */
    @PostMapping("/updateCarouselEnd")
    public String updateCarouselEnd(@RequestBody Carousel carousel) {
        return carouselService.updateCarouselEnd(carousel);
    }

    /**
     * //修改轮播图图片
     * @author 徐
     * @param id 轮播图id
     * @date 2025-07-28
     * @return 受影响的行数
     */

    @PostMapping("/upload1End")
    public Rep updateCarouselImgEnd(@RequestParam("file") MultipartFile file, @RequestParam("id") Integer id) throws Exception {
        // 1,存储上传的文件信息
        InputStream fileInputStream=file.getInputStream();
        String fileName=System.currentTimeMillis()+"/"+file.getOriginalFilename();
        System.out.println("id ============== " + id);
        // 用户头像对象
        Carousel carousel=new Carousel();
        // 将路径存入对象中
        carousel.setImageurl(fileName);
        carousel.setId(id);
        // 调用修改用户头像的方法
        this.carouselService.updateCarouselImgEnd(carousel);
        //this.userimgsService.save(userimgs);
        // 2,上传到minio服务器
        ObjectWriteResponse objectWriteResponse=minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket("moviebucket")
                        .object(fileName)
                        .stream(fileInputStream,-1L,10485760L)
                        .build()
        );
        return new Rep().addData("fileMessage",objectWriteResponse.object());
    }

    /**
     * //批量新增轮播图图片
     * @author 徐
     * @param carouselList 轮播图图片集合
     * @date 2025-07-28
     * @return 受影响的行数
     */
    @PostMapping("/addCarouselImgEnd")
    public String addCarouselImgEnd(List<Carousel> carouselList) {
        int addCarouselImgEnd = carouselService.addCarouselImgEnd(carouselList);
        if (addCarouselImgEnd > 0){
            return "success";
        }else {
            return "fail";
        }
    }

    /**
     * 新增轮播图
     * @param file 文件对象
     * @return rep对象
     * @throws Exception 抛出异常
     * 徐
     */
    @PostMapping("/upload3")
    public Rep upload3(@RequestParam("file") MultipartFile file) throws Exception {
        // 1,存储上传的文件信息
        InputStream fileInputStream=file.getInputStream();
        String fileName=System.currentTimeMillis()+"/"+file.getOriginalFilename();
        // 用户头像对象
        Carousel carousel=new Carousel();
        // 将路径存入对象中
        carousel.setImageurl(fileName);
        // save[存入的方法]
        this.carouselService.sava(carousel);
        // 2,上传到minio服务器
        ObjectWriteResponse objectWriteResponse=minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket("moviebucket")
                        .object(fileName)
                        .stream(fileInputStream,-1L,10485760L)
                        .build()
        );
        return new Rep().addData("fileMessage",objectWriteResponse.object());
    }

    /**
     * //后台根据id查询轮播图是否存在
     * @author 徐
     * @param id 轮播图id
     * @date 2025-08-04
     * @return 受影响行数
     */
    @GetMapping("/selectById")
    public Rep selectById(@RequestParam("id") Integer id) {
        Carousel selectById = this.carouselService.selectById(id);
        return new Rep().addData("data",selectById);
    }


}
